package org.chen.wordwave.base.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.chen.wordwave.R;

public class TargetView extends LinearLayout implements View.OnClickListener {

    private TextView output;


    private Button copy;


    private final Context mContext;


    private TargetViewListener mListener;


    public void setTargetViewListener(TargetViewListener listener) {
        mListener = listener;
    }

    public TargetView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.targetview_layout, this);
        initViews();
    }

    private void initViews() {
        output = findViewById(R.id.output);
        output.setMovementMethod(ScrollingMovementMethod.getInstance());
        copy = findViewById(R.id.copy);
        output.setOnClickListener(this);
        copy.setOnClickListener(this);
        copy.setVisibility(GONE);
    }

    public void setCopyBtnVisibility(int visiblility) {
        this.copy.setVisibility(visiblility);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.output) {
        } else if (id == R.id.copy) {
            String result = output.getText().toString();
            mListener.onCopy(result);
        } else {
            throw new IllegalStateException("Unexpected value: " + view.getId());
        }
    }
    public TextView getTargetTextView(){
        return output;
    }

    public interface TargetViewListener {

        void onCopy(String text);

    }
}
